package com.zycfc.zsf.boot.util.bean;

import java.util.regex.*;
import com.zycfc.zsf.boot.util.text.*;

public class ValidateMore
{
    private static final String REGEX_MOBILE_SIMPLE = "^[1]\\d{10}$";
    private static final Pattern PATTERN_REGEX_MOBILE_SIMPLE;
    private static final String REGEX_MOBILE_EXACT = "^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|(147))\\d{8}$";
    private static final Pattern PATTERN_REGEX_MOBILE_EXACT;
    private static final String REGEX_TEL = "^(\\d{3,4}-)?\\d{6,8}$";
    private static final Pattern PATTERN_REGEX_TEL;
    private static final String REGEX_ID_CARD15 = "^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$";
    private static final Pattern PATTERN_REGEX_ID_CARD15;
    private static final String REGEX_ID_CARD18 = "^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9Xx])$";
    private static final Pattern PATTERN_REGEX_ID_CARD18;
    private static final String REGEX_EMAIL = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
    private static final Pattern PATTERN_REGEX_EMAIL;
    private static final String REGEX_URL = "[a-zA-z]+://[^\\s]*";
    private static final Pattern PATTERN_REGEX_URL;
    private static final String REGEX_DATE = "^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$";
    private static final Pattern PATTERN_REGEX_DATE;
    private static final String REGEX_IP = "((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)";
    private static final Pattern PATTERN_REGEX_IP;
    
    public static boolean isMobileSimple(final CharSequence input) {
        return isMatch(ValidateMore.PATTERN_REGEX_MOBILE_SIMPLE, input);
    }
    
    public static boolean isMobileExact(final CharSequence input) {
        return isMatch(ValidateMore.PATTERN_REGEX_MOBILE_EXACT, input);
    }
    
    public static boolean isTel(final CharSequence input) {
        return isMatch(ValidateMore.PATTERN_REGEX_TEL, input);
    }
    
    public static boolean isIdCard(final CharSequence input) {
        return isMatch(ValidateMore.PATTERN_REGEX_ID_CARD15, input) || isMatch(ValidateMore.PATTERN_REGEX_ID_CARD18, input);
    }
    
    public static boolean isEmail(final CharSequence input) {
        return isMatch(ValidateMore.PATTERN_REGEX_EMAIL, input);
    }
    
    public static boolean isUrl(final CharSequence input) {
        return isMatch(ValidateMore.PATTERN_REGEX_URL, input);
    }
    
    public static boolean isDate(final CharSequence input) {
        return isMatch(ValidateMore.PATTERN_REGEX_DATE, input);
    }
    
    public static boolean isIp(final CharSequence input) {
        return isMatch(ValidateMore.PATTERN_REGEX_IP, input);
    }
    
    public static boolean isMatch(final Pattern pattern, final CharSequence input) {
        return !StringUtils.isEmpty(input) && pattern.matcher(input).matches();
    }
    
    public static int nonNegative(final String role, final int x) {
        if (x < 0) {
            throw new IllegalArgumentException(role + " (" + x + ") must be >= 0");
        }
        return x;
    }
    
    private static void mustBiger(final String role, final Number number) {
        if (null == number) {
            throw new IllegalArgumentException(role + " (" + number + ") is null");
        }
        if (number.intValue() < 0) {
            throw new IllegalArgumentException(role + " (" + number + ") must be >= 0");
        }
    }
    
    public static Integer nonNegative(final String role, final Integer x) {
        mustBiger(role, x);
        return x;
    }
    
    public static long nonNegative(final String role, final long x) {
        mustBiger(role, x);
        return x;
    }
    
    public static Long nonNegative(final String role, final Long x) {
        mustBiger(role, x);
        return x;
    }
    
    public static double nonNegative(final String role, final Double x) {
        if (null == x) {
            throw new IllegalArgumentException(role + " (" + x + ") is null");
        }
        if (Double.isNaN(x)) {
            throw new IllegalArgumentException(role + " (" + x + ") is NaN");
        }
        if (x < 0.0) {
            throw new IllegalArgumentException(role + " (" + x + ") must be >= 0");
        }
        return x;
    }
    
    static {
        PATTERN_REGEX_MOBILE_SIMPLE = Pattern.compile("^[1]\\d{10}$");
        PATTERN_REGEX_MOBILE_EXACT = Pattern.compile("^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|(147))\\d{8}$");
        PATTERN_REGEX_TEL = Pattern.compile("^(\\d{3,4}-)?\\d{6,8}$");
        PATTERN_REGEX_ID_CARD15 = Pattern.compile("^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$");
        PATTERN_REGEX_ID_CARD18 = Pattern.compile("^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9Xx])$");
        PATTERN_REGEX_EMAIL = Pattern.compile("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$");
        PATTERN_REGEX_URL = Pattern.compile("[a-zA-z]+://[^\\s]*");
        PATTERN_REGEX_DATE = Pattern.compile("^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$");
        PATTERN_REGEX_IP = Pattern.compile("((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)");
    }
}
